package business_logic.game_elements;


import java.util.Collection;
import java.util.EnumSet;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import business_logic.GameElement;
import business_logic.Tile;
import business_logic.view.GameViewIdentifier;

import model.collisions.CollisionCategory;
import model.elements.NonPlayerCharacter;
import model.elements.ArtificialIntelligence.Tank;
import model.elements.attributes.Coordinate2D;
import model.elements.attributes.Damage;
import model.elements.attributes.Lives;
import model.elements.attributes.Speed;
import model.elements.configurations.NonPlayerCharacterConfig;
import model.elements.states.weapon.BulletEquipped;
import model.exceptions.ModelExceptions;
import model.map.ElementsMap;

public final class LopezReggae extends NonPlayerCharacter implements GameElement
{
	
	private static final Collection<GameViewIdentifier> m_viewIdentifiers = 
            EnumSet.of(GameViewIdentifier.SPRITE_LOPEZ_REGGAE);
	
	private static final int LopezRegaeBaseAmmo 		 	= 1;
	
    public LopezReggae(Coordinate2D startingPosition, ElementsMap map) throws ModelExceptions
    {
        super(startingPosition, new ConfigLopezRegae(map));
        super.setWeaponState(new BulletEquipped(LopezRegaeBaseAmmo, this));
    }

    public Collection<GameViewIdentifier> getViewIdentifiers()
    {
        return m_viewIdentifiers;
    }

    @Override
    public void vivir()
    {
        try
        {
            this.update();
        }
        catch (ModelExceptions e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public int getX()
    {
        return Tile.pixelToTile( m_currentPosition.getRealX() );
    }

    @Override
    public int getY()
    {
        return Tile.pixelToTile( m_currentPosition.getRealY() );
    }

    private static final long m_serialVersionUID = 1L;
    
    @Override
    public Element save()
    {
        Element elem = DocumentHelper.createElement( this.getClass().getName() );
        elem.addAttribute( "serialVersion", m_serialVersionUID+"" );
        elem.addAttribute( "x", Math.floor( this.getPosition().getRealX() )+0.5 +"" );
        elem.addAttribute( "y", Math.floor( this.getPosition().getRealY() )+0.5 +"" );
        elem.addAttribute( "lives", this.m_lives.getValue()+"" );
        
        return elem;
    }
    
    public LopezReggae( Element elem ) throws ModelExceptions
    {
        super
        (
            new Coordinate2D
            (
                Double.parseDouble(elem.attributeValue("x")),
                Double.parseDouble(elem.attributeValue("y"))
            ),
            new ConfigLopezRegae( null )
        );
        
        super.setWeaponState( new BulletEquipped(LopezRegaeBaseAmmo, this) );
        this.m_lives.setValue( Integer.parseInt(elem.attributeValue("lives")) );
    }
}

class ConfigLopezRegae extends NonPlayerCharacterConfig
{
	private static final int 				LopezRegaeHitPoints 		= 10;
	private static final double 			LopezRegaeBaseSpeed 		= 0.005;
	private static final int 				LopezRegaeContactDamage 	= 5;
	private static final CollisionCategory 	LopezRegaeCollisionCategory = CollisionCategory.C_NPC;
	
	public ConfigLopezRegae( ElementsMap map) {
		m_lives 		 = new Lives(LopezRegaeHitPoints);
		m_speed 		 = new Speed(LopezRegaeBaseSpeed);
		m_equippedWeapon = null;
		m_IA 			 = new Tank( Tile.speedToTickets( LopezRegaeBaseSpeed ), map );
		m_damage 		 = new Damage(LopezRegaeContactDamage);
		m_category 		 = LopezRegaeCollisionCategory;
	}
}